rm(list = ls())
library(tidyverse)
library(haven)
library(xtable)
# Figure A.1: Time Trend of Government Subsidies####
vis_time_trend = read_csv( "~/Dropbox/revolving_door_jmp/replication_files/subsidy_trend.csv")

# Value used to transform the data
coeff <- 90

# A few constants
Color <- "#69b3a2"
  priceColor <- rgb(0.2, 0.6, 0.9, 1)
  
  ggplot(vis_time_trend, aes(x= year)) +
    geom_bar( aes(y=subsidy_total), stat="identity", linewidth=.1, fill= Color, color="black", alpha=.5) + 
    geom_line( aes(y= gdp_pc / coeff), size=.8, color=priceColor) +
    scale_y_continuous(
      # Features of the first axis
      name = "Subsidy Value (Billion RMB)",
      # Add a second axis and specify its features
      sec.axis = sec_axis(~.*coeff, name="GDP Per Capita")
    ) + 
    scale_x_continuous(breaks=seq(2007, 2019, 1))+
    theme_classic(15) +
    theme(
      axis.title.y = element_text(color = Color, size=13),
      axis.title.y.right = element_text(color = priceColor, size=13)
    ) 
  
  # compute the growth rate
  vis_time_trend %>% mutate(x = subsidy_total/lag(subsidy_total)-1) %>% 
    summarise(mean = mean(x,na.rm =T))
  
ggsave("/Users/zerenli1992/Dropbox/Apps/Overleaf/subsidies_for_sale_2020/total_subsidy_time_trend.png",height = 4,width =5.5)
  
  

# Table A.1: Retired vs Incumbent Officials Who Fell During the Anticorruption Campaign ####
probe_wang <- read_excel("~/Dropbox/revolving_door_jmp/replication_files/anti_corruption_wang.xlsx") %>% 
  mutate(retire = if_else(str_detect(name, "退休"),1,0)) 
  
questionr::describe(probe_wang$retire)

# Table A.2: Firm-level Summary Statistics####
read_dta("/Users/zerenli1992/Dropbox/revolving_door_jmp/replication_files/firm_yr_panel.dta") %>% 
  filter(year %in% 2007:2019) %>% 
  select(lntfp,roa,prof_m,rd_n,subsidy_total,TotalAsset, TotalLiab) %>% 
  mutate(TotalAsset =round( TotalAsset/1000000,3),
         TotalLiab = round( TotalLiab/1000000,3),
         subsidy_total = round(subsidy_total/1000000,3)) %>% 
  as.data.frame() %>% 
  stargazer::stargazer(. ,
                       title = "Firm-level Summary Statistics",
                       label = "firm_summary",
                       # type = "text",
                       digits = 2,
                       style = "qje",
                       covariate.labels = c("Total Factor Productivity (logged)", "ROA","Profit Margin" ,"Revolving Door", "Subsidy Size (Million RMB)","Firm Size (Million RMB)","Liability (Million RMB)"),
                       omit.summary.stat = c("p25", "p75")) 



#Table A.3: Summary Statistics of Government Subsidies ####
subsidy_df = read_dta("~/Dropbox/revolving_door_jmp/replication_files/program_replication_jop.dta") %>% 
  filter(year %in% 2007:2019)
  
  
 subsidy_df %>% 
  group_by(level) %>% 
  summarise(Observation = n(),
            mean = mean(subsidy_size)/1000000,
            sd = sd(subsidy_size)/1000000) %>% 
  xtable()




  